package com.ekingstar.hedss.action;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.UserExistException;
import com.ekingstar.hedss.domain.HedssZllb;
import com.ekingstar.hedss.service.IHedssService;

@SuppressWarnings({"serial","unchecked"})
@Action(name="addOrUpdateZllb",namespace="/documentation")
@Results({
	@Result(name = "input", value = "/pages/documentation/zllb-input.jsp"),	 
	@Result(name="list",value="documentation",type=ServletActionRedirectResult.class,params={"namespace","/documentation"})
		})
public class AddOrUpdateZllbAction extends ActionSupport implements Preparable {
	private IHedssService hedssService;
	
	private Map userTypes;
	

	public Map getUserTypes() {
		return userTypes;
	}

	public void setUserTypes(Map userTypes) {
		this.userTypes = userTypes;
	}

	private HedssZllb zllb;
	
	public String input(){
		if (zllb != null && zllb.getId() !=null){
			zllb = hedssService.viewZllb(zllb.getId());
		}
		
		return "input";
	}
	
	@Validations(
			requiredStrings={
					@RequiredStringValidator(message="资料类别名称必填",fieldName="zllb.name")},
					
			regexFields={
					}
			)
	public String execute(){
		
		boolean logged = UserUtil.isLogged();
		if (!logged ){
			return "nologin";
		}
		User user= UserUtil.getCurrentUser();
		
		try{
			if(zllb.getMemo()!=null&&zllb.getMemo().length()>150){
				this.addFieldError("zllb.memo","备注过长。");
				return "input";
			}
			if (zllb != null && zllb.getId() != null && !zllb.getId().trim().equals("")){
				zllb.setLastUpdateDate(new Date());
				zllb.setUpdaterId(user.getName());
				hedssService.updateZllb(zllb);
			}else {
				zllb.setAddDate(new Date());
				zllb.setAdderId(user.getName());
				hedssService.addZllb(zllb);
			}
		}catch(UserExistException e){
			this.addFieldError(ERROR,e.getMessage());
			return "input";
		}
		
		return "list";
	}
	
	public IHedssService getHedssService() {
		return hedssService;
	}

	public void setHedssService(IHedssService hedssService) {
		this.hedssService = hedssService;
	}

	public HedssZllb getZllb() {
		return zllb;
	}

	public void setZllb(HedssZllb zllb) {
		this.zllb = zllb;
	}

	public void prepare() throws Exception {

		//universitys =  CodeUtil.getInctance().getCodeNames("dm_gx");
		Map tmp = new HashMap();
		tmp.put("", "----请选择----");
		if (userTypes == null) {
			userTypes = new LinkedHashMap();
			User.Type[] uts = User.Type.values();
			for (int i = 0 ;i  < uts.length ; i++){
				userTypes.put(uts[i].name(), uts[i].toString());
			}
		}
		
	}


	
}
